<script>
import {getAddClass, getCollege} from "@/utils/class";
import {getTeacher} from "@/utils/class/counselor";

export default {
  name: "addClass",
  data() {
    return {
      form: {
        collegeName: '',
        className: '',
        teacherName: ''
      },
      dialogFormVisible: false,
      formLabelWidth: '80px',
      //学校、负责老师
      collegeId: [],
      teacherList: [],
      //接口参数
      date: {
        name: '',
        deptId: '',
        userId: '',
      },
    };
  },
  methods: {
    // 添加 ：学院、老师
    changeCollege(val) {
      //console.log(val)
      this.date.deptId = val
    },
    changeTeacher(val) {
      //console.log(val)
      this.date.userId = val
    },
    //添加按钮
    increase() {
      this.dialogFormVisible = true;
    },
    //添加完保存
    add() {
      this.dialogFormVisible = false
      this.date.name = this.form.className
      getAddClass(this.date).then(res => {
        //console.log(res)
        this.$message.success('添加成功')
      })
    }
  },
  created() {
    //学校名称
    getCollege().then(res => {
      //console.log(res)
      this.collegeId = res.data
    })
    //负责老师
    getTeacher().then(res => {
      // console.log(res)
      this.teacherList = res.rows
    })
  }
}
</script>

<template>
  <div>
    <div class="add-main">
      <!-- Form -->
      <el-button type="primary" @click="increase">添加</el-button>
      <el-dialog :center="true" :close-on-click-modal="false"
                 :visible.sync="dialogFormVisible"
                 width="430px">
        <el-form :model="form">
          <el-form-item :label-width="formLabelWidth" label="学院名称">
            <el-select v-model="form.collegeName" placeholder="请输入学院名称" style="width: 280px"
                       @change="changeCollege(form.collegeName)">
              <el-option v-for="item in this.collegeId" :label="item.deptName" :value="item.deptId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="班级名称">
            <el-input v-model="form.className" autocomplete="off" placeholder="请输入班级名称"
                      style="width: 280px"></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="负责老师">
            <el-select v-model="form.teacherName" placeholder="请选择负责老师" style="width: 280px"
                       @change="changeTeacher(form.teacherName)">
              <el-option v-for="item in this.teacherList" :label="item.nickName"
                         :value="item.userId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible=false">取 消</el-button>
          <el-button type="primary" @click="add">保 存</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<style lang="scss" scoped>

</style>
